//
//  UIApplicationExtension.swift
//  XiaoweHearingAid
//
//  Created by Riven on 2023/4/10.
//

import UIKit

public extension UIApplication {
    
    class var appKeyWindow: UIWindow? {
        var window: UIWindow?
        if #available(iOS 13.0, *) {
            window = shared.windows.filter({ $0.isKeyWindow }).last
        } else {
            window = shared.delegate?.window ?? shared.keyWindow
        }
        return window
    }
    
    /// The key window of the application, if one exists.
//    var keyWindow: UIWindow? {
//        let scenes = connectedScenes.compactMap { $0 as? UIWindowScene }
//        
//        for scene in scenes {
//            if let keyWindow = scene.windows.first(where: { $0.isKeyWindow }) {
//                return keyWindow
//            }
//        }
//        
//        return nil
//    }
    
    /// Ends editing on the key window of the application.
    class func endEditing() {
        appKeyWindow?.endEditing(true)
    }
    
    class var appRootViewController: UIViewController? {
        appKeyWindow?.rootViewController
    }
    
    class var appTopViewController: UIViewController? {
        let window = UIApplication.appKeyWindow
        if var topViewController = window?.rootViewController {
            while true {
                if let controller = topViewController.presentedViewController {
                    topViewController = controller
                }else if let navController = topViewController as? UINavigationController,
                         let controller = navController.topViewController {
                    topViewController = controller
                }else if let tabbarController = topViewController as? UITabBarController,
                         let controller = tabbarController.selectedViewController {
                    topViewController = controller
                }else {
                    break
                }
            }
            return topViewController
        }
        return nil
    }
    
    /// 获取最底层展示的控制器
    class func bottomestPresentedViewController(_ targetVC: UIViewController?) -> UIViewController? {
        guard var bottomestVC = targetVC else {
            return nil
        }
        while bottomestVC.presentingViewController != nil {
            bottomestVC = bottomestVC.presentingViewController!
        }
        return bottomestVC
    }
        
    /// 获取最顶层展示的控制器
    class func topestPresentedViewController(_ targetVC: UIViewController?) -> UIViewController? {
        guard var topestVC = targetVC else {
            return nil
        }
        while topestVC.presentedViewController != nil {
            topestVC = topestVC.presentedViewController!
        }
        return topestVC
    }
    
    /// 所有页面都diss到根目录
    class func dismissToRoot(_ targetVC: UIViewController, completion: (() -> Void)? = nil){
        let vc = bottomestPresentedViewController(targetVC)
        
        vc?.dismiss(animated: true, completion: completion)
    }
    
}
